package com.able.concurrency.t1;

import java.util.concurrent.TimeUnit;

/**
 * @author abel.he
 * @data 2018/8/15 18:46
 * @describe 同步方法只影响锁定同一个对象的同步方法，不影响其他线程调用其他非同步方法或调用其他锁资源的同步方法
 */
public class Test4 {

    private Object object = new Object();

    private synchronized void m1() {
        System.out.println("private synchronized void m1() start");
        try {
            TimeUnit.SECONDS.sleep(3);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("private synchronized void m1() end");
    }

    private void m2() {
        synchronized (object) {
            System.out.println("private void m2() start");
            try {
                Thread.sleep(1500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("private void m2() end");
        }
    }

    private void m3() {
        System.out.println("private void m3() start ");
        try {
            Thread.sleep(1500);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("private void m3() end ");
    }

    public static class MyThread01 implements Runnable{

        private Test4 test4;
        private int num;

        public MyThread01(Test4 test4, int num) {
            this.test4 = test4;
            this.num = num;
        }

        public void run() {
            if (num < 0) {
                test4.m1();
            } else if (num == 0) {
                test4.m2();
            } else {
                test4.m3();
            }
        }
    }

    public static void main(String[] args) {
        Test4 test4 = new Test4();
        new Thread(new MyThread01(test4, -1)).start();
        new Thread(new MyThread01(test4, 0)).start();
        new Thread(new MyThread01(test4, 1)).start();
    }

}
